package com.esun.server.exceptionHandler;


import cn.dev33.satoken.exception.NotLoginException;
import com.esun.common.enums.Message;
import com.esun.common.exception.AsyncException;
import com.esun.common.exception.CustomException;
import com.esun.common.response.RestResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 自定义异常处理类
 *
 * @author john.xiao
 * @date 2020/9/1 11:02
 */
@RestControllerAdvice
public class CustomHttpExceptionHandler {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@CrossOrigin
	@ExceptionHandler(CustomException.class)
	public RestResponse handleCustomHttpException(CustomException e) {
		RestResponse restResponse = new RestResponse();
		if (!StringUtils.isEmpty(e.getMessage())) {
			restResponse.setResultCode(e.getCode());
			restResponse.setResultMsg( e.getMsg());
			return restResponse;
		}
		logger.error(e.getMessage(), e);
		return RestResponse.FAILED;
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public RestResponse handlerNoFoundException(Exception e) {
		logger.error(e.getMessage(), e);
		RestResponse restResponse = new RestResponse();
		restResponse.setResultCode("404");
		restResponse.setResultMsg("请求路径不存在");
		return restResponse;
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public RestResponse handleDuplicateKeyException(DuplicateKeyException e) {
		logger.error(e.getMessage(), e);
		RestResponse restResponse = new RestResponse();
		restResponse.setResultCode(Message.QUERY_SUCCESS.getCode());
		restResponse.setResultMsg("数据库中已存在该记录");
		return restResponse;
	}

	@ExceptionHandler(NullPointerException.class)
	public RestResponse handleNullPointerException(NullPointerException e) {
		logger.error(e.getMessage(), e);
		RestResponse restResponse = new RestResponse();
		restResponse.setResultCode(Message.QUERY_SUCCESS.getCode());
		restResponse.setResultMsg("空指针异常");
		return restResponse;
	}

	@ExceptionHandler(Exception.class)
	public RestResponse handleException(Exception e) {
		logger.error(e.getMessage(), e);
		RestResponse restResponse = new RestResponse();
		restResponse.setResultCode(Message.QUERY_SUCCESS.getCode());
		restResponse.setResultMsg(e.getMessage());
		return restResponse;
	}

	@ExceptionHandler(AsyncException.class)
	public RestResponse handlerAsyncException(Exception e) {
		logger.error(e.getMessage(), e);
		RestResponse restResponse = new RestResponse();
		restResponse.setResultCode(Message.QUERY_SUCCESS.getCode());
		restResponse.setResultMsg(e.getMessage());
		return restResponse;
	}

	@ExceptionHandler(NotLoginException.class)
	public RestResponse handlerNotLoginException(Exception e) {
		logger.error(e.getMessage(), e);
		RestResponse restResponse = new RestResponse();
		restResponse.setResultCode(Message.QUERY_SUCCESS.getCode());
		restResponse.setResultMsg("未登录，请先登录后再进行访问");
		return restResponse;
	}
}
